import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class XMLConverter {
	public static void main(String[] args) throws IOException{
		File curDirectory = new File(".");
		File backupDir = new File(".\\backup");
		backupDir.mkdir();

		String curDirFiles[] = curDirectory.list(); // 현재 디렉토리의 파일리스트.

		int targetIndex = 0;

		for (targetIndex = 0; targetIndex < curDirFiles.length - 1; targetIndex++) {
			if (curDirFiles[targetIndex].length() < 4)
				continue;
			String temp = (String) curDirFiles[targetIndex].subSequence(
					curDirFiles[targetIndex].length() - 4,
					curDirFiles[targetIndex].length());
			if (!temp.equalsIgnoreCase(".xml"))
				continue;

			String srcXMLFileNm = curDirFiles[targetIndex]; // 입력파일명
			File srcFile = new File(srcXMLFileNm);
			srcFile.renameTo(new File(".\\backup\\"+curDirFiles[targetIndex]));
			curDirFiles[targetIndex] = ".\\backup\\"+curDirFiles[targetIndex];
		}

		for (targetIndex = 0; targetIndex < curDirFiles.length - 1; targetIndex++) {

			if (curDirFiles[targetIndex].length() < 4)
				continue;
			String temp = (String) curDirFiles[targetIndex].subSequence(
					curDirFiles[targetIndex].length() - 4,
					curDirFiles[targetIndex].length());
			if (!temp.equalsIgnoreCase(".xml"))
				continue;

			String srcXMLFileNm = curDirFiles[targetIndex]; // 입력파일명
			String trgXMLFileNm = curDirFiles[targetIndex].replace(".\\backup\\",
					""); // 입력파일명

			File srcFile = new File(srcXMLFileNm);
			File trgFile = new File(trgXMLFileNm);

			FileReader srcReader = new FileReader(srcFile);
			FileWriter trgWriter = new FileWriter(trgFile);

			BufferedReader readBuffer = new BufferedReader(srcReader);
			BufferedWriter writeBuffer = new BufferedWriter(trgWriter);


			String currentLineStr = null;

			boolean isEnd = false;
			boolean isIn = false;

			while ((currentLineStr = readBuffer.readLine()) != null) {
				writeBuffer.write(currentLineStr.getBytes("utf-8") + "\n");
				if (isEnd)
					continue;

				if (isIn == true) {
					String cmp1 = currentLineStr.toLowerCase().replace("{", "");
					if (!currentLineStr.toLowerCase().equals(cmp1)) {
						writeBuffer.write("\tgfn_ResizeInit(990,785);\n");
						isEnd = true;
					}
				}

				String checkInclude = currentLineStr.replace(
						"BSAC_JS::bsac_common.js", "");
				String checkOnLoad = currentLineStr.replace("function", "");

				if (!checkInclude.equalsIgnoreCase(currentLineStr)) {
					writeBuffer
							.write("#include \"BSAC_JS::bsac_resize.js\";\n");
				}
				if (!checkOnLoad.equalsIgnoreCase(currentLineStr)) {
					checkOnLoad = currentLineStr;
					String cmp1 = checkOnLoad.toLowerCase();
					String cmp2 = currentLineStr.toLowerCase();
					cmp1 = cmp1.replace("onloadcompleted", "");
					if (!cmp1.equalsIgnoreCase(cmp2)) {
						cmp1 = cmp2.replace("{", "");
						if (!cmp1.equalsIgnoreCase(cmp2)) {
							writeBuffer.write("\tgfn_ResizeInit(990,785);\n");
							isEnd = true;
						} else {
							isIn = true;
						}
					}
				}
			}

			readBuffer.close();
			writeBuffer.close();
			srcReader.close();
			trgWriter.close();
		}
	}
}
